package com.pig4cloud.pig.buyan.controller;

import cn.hutool.core.collection.CollUtil;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.buyan.entity.CarModel;
import com.pig4cloud.pig.buyan.service.ModelService;
import com.pig4cloud.pig.common.core.util.R;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.http.HttpHeaders;
import org.springdoc.api.annotations.ParameterObject;

import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/carModel")
@Tag(description = "model", name = "汽车型号模块")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ModelController {
	private final  ModelService modelService;
	//分页查询
	@GetMapping("/page")
	public R getModelPage(@ParameterObject Page page, @ParameterObject CarModel model) {
		LambdaQueryWrapper<CarModel> wrapper = Wrappers.<CarModel>lambdaQuery()
				.eq(StrUtil.isNotBlank(model.getDelFlag()),CarModel::getDelFlag,model.getDelFlag());
		return R.ok(modelService.page(page, wrapper));
	}
	@PostMapping("/insert")
	//新增
	public R save(@RequestBody CarModel model){
		return R.ok(modelService.save(model));
	}
	//更新
	@PutMapping("/update")
	public R updateById(@RequestBody CarModel model){
		if(modelService.getById(model.getModelId())!=null){
			return R.ok(modelService.updateById(model));
		}else {
			return R.failed("更新失败");
		}
	}
	@DeleteMapping("/del/{ids}")
	public R removeByIds(@PathVariable Long[] ids){
		return R.ok(modelService.removeBatchByIds(CollUtil.toList(ids)));
	}
	@GetMapping("/select/{modelId}")
	public R getById(@PathVariable Long modelId){
		return R.ok(modelService.getById(modelId));
	}

}
